package com.excellent.item.service.impl;

import com.excellent.item.mapper.SpecGroupMapper;
import com.excellent.item.mapper.SpecParamMapper;
import com.excellent.item.pojo.SpecGroup;
import com.excellent.item.pojo.SpecParam;
import com.excellent.item.service.SpecificationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Xxacker
 */
@Slf4j
@Service("SpecificationService")
public class SpecificationServiceImpl implements SpecificationService {

    @Autowired
    private SpecGroupMapper groupMapper;

    @Autowired
    private SpecParamMapper paramMapper;

    /**
     * 根据条件查询规格参数
     * @param gid
     * @return
     */
    public List<SpecParam> queryParams(Long gid, Long cid, Boolean generic, Boolean searching) {
        SpecParam param = new SpecParam();
            param.setGroupId(gid);
            param.setCid(cid);
            param.setGeneric(generic);
            param.setSearching(searching);
        List<SpecParam> specParamList = this.paramMapper.select(param);
        log.info("规格参数列表 specParamList：" + specParamList);
        return specParamList;
    }

    /**
     * 根据分类id查询规格参数分组
     * @param cid
     * @return
     */
    public List<SpecGroup> queryGroupsByCid(Long cid) {
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(cid);
        return this.groupMapper.select(specGroup);
    }

    /**
     * 查询规格参数组，及组内参数
     * */
    public List<SpecGroup> querySpecsByCid(Long cid) {
        // 查询规格组
        List<SpecGroup> groups = this.queryGroupsByCid(cid);
        groups.forEach(group -> {
            // 查询组内参数
            group.setParams(this.queryParams(group.getId(), null, null, null));
        });
        return groups;
    }

    @Override
    public void saveSpecGroup(SpecGroup specGroup) {
        this.groupMapper.insertSelective(specGroup);
    }

    @Override
    public void updateSpecGroup(SpecGroup specGroup) {
        this.groupMapper.updateSpecGroup(specGroup);
    }

    @Override
    public void deleteSpecGroup(Long id) {
        this.groupMapper.deleteById(id);
    }

    @Override
    public void saveSpecParam(SpecParam specParam) {
        this.paramMapper.insertSelective(specParam);
    }

    @Override
    public void updateSpecParam(SpecParam specParam) {
        this.paramMapper.updateByPrimaryKey(specParam);
    }

    @Override
    public void deleteSpecParam(Long id) {
        this.paramMapper.deleteById(id);
    }

}
